
import numpy as np

# 读取数据集
data = [
    # (user_id, course_id, gpa)
    (1, 1001, 3.0),
    (1, 1002, 4.0),
    (1, 1003, 3.5),
    (2, 1001, 4.0),
    (2, 1002, 3.5),
    (2, 1003, 4.0),
    (3, 1001, 3.5),
    (3, 1002, 3.0),
    (3, 1003, 3.5),
    (4, 1001, 4.0),
    (4, 1002, 4.0),
    (4, 1003, 3.5),
    (5, 1001, 3.5),
    (5, 1002, 4.0),
    (5, 1003, 4.0)
]

# 建立用户-课程的关系矩阵
user_ids = list(set([d[0] for d in data]))
course_ids = list(set([d[1] for d in data]))
user_index = {user_ids[i]: i for i in range(len(user_ids))}
print(user_index)
course_index = {course_ids[i]: i for i in range(len(course_ids))}
print(course_index)
R = np.zeros((len(user_ids), len(course_ids)))
for d in data:
    user_id = d[0]
    course_id = d[1]
    gpa = d[2]
    R[user_index[user_id], course_index[course_id]] = gpa
print(R)

# 建立课程-课程的关系矩阵
C = np.corrcoef(R.T)
# print(C)